package data.implementation;

import data.ISoinDAO;
import data.MySingleton;
import exception.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import model.Soin;

/**
 *
 * @author BACQ Justine et SACRE Sauvane
 */

public class SoinDBAccess implements ISoinDAO{
    private static final String allSoin = "SELECT DISTINCT typeSoin FROM soin";
    private static final String getSoin = "SELECT * FROM soin WHERE idSoin = ? ";
    
    @Override
    public List<Soin> getAllSoin() throws ExceptionConnection, SoinDataErrorException {
        ArrayList<Soin> tabSoin = new ArrayList<Soin>();
        Connection con = MySingleton.getInstance(); 
        PreparedStatement stat;
        try{
            stat = con.prepareStatement(allSoin);
            ResultSet data = stat.executeQuery();
            while(data.next()){
                Soin s = new Soin(data.getString("typeSoin"));
                tabSoin.add(s); 
            }
            stat.close();
        }catch(SQLException ex){
            throw new SoinDataErrorException();
        }
        return tabSoin;
    }
    
    @Override
    public Soin getSoin(String idSoin) throws SoinRecupException, ExceptionConnection{
        Connection con = MySingleton.getInstance();
        Soin soin=null; 
        try{
            PreparedStatement prepStat = con.prepareStatement(getSoin);
            prepStat.setString(1, idSoin);
            ResultSet res = prepStat.executeQuery();
            
            if(res.next()){
                soin=new Soin(idSoin, res.getString("nomSoin"), res.getString("typeSoin"));
            }
        } catch(SQLException ex){
            throw new SoinRecupException();
        }
        return soin;
    }
}
